package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 检查及扣减账户余额
     *
     * @param accountDeductVo
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "检查及扣减账户余额")
    @PostMapping("/checkAndDeduct")
    public Result checkAndDeduct(@RequestBody AccountLockVo accountDeductVo) {
        //1.从ThreadLocal中获取用户ID
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            accountDeductVo.setUserId(userId);
        }
        //2.调用业务逻辑完成账户余额扣款
        userAccountService.checkAndDeduct(accountDeductVo);
        return Result.ok();
    }

    /**
     * 初始化金额
     *
     * @param userId
     * @return
     */
    @Operation(summary = "初始化金额")
    @GetMapping("initUserAccount/{userId}")
    public Result initUserAccount(@PathVariable Long userId) {
        //	远程调用。
        userAccountService.initUserAccount(userId);
        //	返回数据
        return Result.ok();
    }

    /**
     * 用户充值记录
     *
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取用户充值记录")
    @GetMapping("/findUserRechargePage/{page}/{limit}")
    public Result findUserRechargePage(@PathVariable Long page,
                                       @PathVariable Long limit) {
        //	获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //	构建Page 对象
        Page<UserAccountDetail> userAccountDetailPage = new Page<>(page, limit);
        //	调用服务层方法.
        IPage<UserAccountDetail> iPage = this.userAccountService.findUserRechargePage(userAccountDetailPage, userId);
        //	返回数据
        return Result.ok(iPage);
    }

    /**
     * 用户消费记录
     *
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取用户消费记录")
    @GetMapping("/findUserConsumePage/{page}/{limit}")
    public Result findUserConsumePage(@PathVariable Long page,
                                      @PathVariable Long limit) {
        //	获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //	构建Page 对象
        Page<UserAccountDetail> userAccountDetailPage = new Page<>(page, limit);
        //	调用服务层方法.
        IPage<UserAccountDetail> iPage = this.userAccountService.findUserConsumePage(userAccountDetailPage, userId);
        //	返回数据
        return Result.ok(iPage);
    }

    /**
     * 检查锁定账户金额
     *
     * @param accountLockVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "检查与锁定账户金额")
    @PostMapping("checkAndLock")
    public Result checkAndLock(@RequestBody AccountLockVo accountLockVo) {
        //	调用服务层方法.
        //		AccountLockResultVo accountLockResultVo = userAccountService.checkAndLock(accountLockVo);
        //		//	最好加个判断
        //		if (null != accountLockResultVo){
        //			//	返回成功.
        //			return Result.ok(accountLockResultVo);
        //		}
        //		//	返回失败
        //		return Result.fail();

        return userAccountService.checkAndLock(accountLockVo);
    }

    /**
     * 获取账户可用余额
     *
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取账号可用金额")
    @GetMapping("getAvailableAmount")
    public Result getAvailableAmount() {
        //	获取账户可用余额
        Long userId = AuthContextHolder.getUserId();
        //	调用服务层方法.
        BigDecimal availableAmount = userAccountService.getAvailableAmount(userId);
        //	返回数据
        return Result.ok(availableAmount);
    }


}

